package com.YCH.user.service.Impl;

import com.YCH.model.article.pojos.TbArticle;
import com.YCH.model.common.dtos.ResponseResult;
import com.YCH.model.common.enums.AppHttpCodeEnum;
import com.YCH.model.mappers.app.TbArticleMapper;
import com.YCH.model.mappers.app.TbUserFollowMapper;
import com.YCH.model.mappers.app.TbUserMapper;
import com.YCH.model.user.pojos.TbUser;
import com.YCH.model.user.pojos.TbUserFan;
import com.YCH.model.user.pojos.TbUserFollow;
import com.YCH.model.user.vo.AuthorInfoVo;
import com.YCH.user.service.AppLoadArticleAuthorService;
import com.YCH.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@SuppressWarnings("all")
public class AppLoadArticleAuthorServiceImpl implements AppLoadArticleAuthorService {
    @Autowired
    private TbArticleMapper tbArticleMapper;
    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private TbUserFollowMapper tbUserFollowMapper;
    @Override
    public ResponseResult loadAuthor(Long articleId) {
        // 1.校验参数
        TbUser user = AppThreadLocalUtils.getUser();
        AuthorInfoVo authorInfoVo = new AuthorInfoVo();
        if (user == null) {
            authorInfoVo.setIsFollow(false);
        }
        Long userId = user.getId();
        // 2.根据文章id查询用户id
        TbArticle tbArticle = tbArticleMapper.loadArticleById(articleId);
        // 3.根据用户id查询作者信息
        TbUser tbUser = tbUserMapper.selectLvByIdDetailed(tbArticle.getUser().getId());
        tbUser.setMobile("");
        tbUser.setEmail("");
        tbUser.setStatus((short) 0);
        authorInfoVo.setTbUser(tbUser);
        // 4.查询是否已经关注该用户
        // 参数一：当前登录用户id，参数二：作者id
        TbUserFollow tbUserFollow = tbUserFollowMapper.selectByFollowId(user.getId(),tbUser.getId());
        if(tbUserFollow==null){
            authorInfoVo.setIsFollow(false);
        }else{
            authorInfoVo.setIsFollow(true);
        }
        return ResponseResult.okResult(authorInfoVo);
    }
}
